using System.Html;
using System.Runtime.CompilerServices;
using LoaTools;
using Nsb;
using Nsb.Classes;
using jQueryApi;

namespace ThisILove
{
    public static class Timer
    {
        public const string QuarterBellKey = "TmrPgQtrBell";
        public const string BellSound = "http://www.rangde.org/static/bell-ring-01.mp3";
        public const string ClinkSound = "http://soundjax.com/reddo/31398%5Eglass-clink-3.mp3";
        public const string WhiperwillSound = "http://soundjax.com/reddo/70117%5Ebird1.mp3";
        public static jQueryObject Indicator;
        public static bool QuarterBell = true;
        public static bool Running;
        public static int Waiter;
        public static jQueryObject TimerText;
        public static jQueryObject TimerWrapper;
        public static jQueryObject Message;
        public static jQueryObject Sound0;
        public static jQueryObject Sound1;
        public static jQueryObject Sound2;
        public static jQueryObject Sound4;

        public static readonly string CatCallSound = Uri.App("md/Sounds/CatCall.mp3");
        public static readonly string BeginSound = Uri.App("md/Sounds/Begin.mp3");
        public static readonly string DoneSound = Uri.App("md/Sounds/Done.mp3");

        public static int ReadyTime = 4000;
        public static int Period = 68000;
        public static int T1 = 150;
        public static int T2 = 2000;
        public static int T3 = 3000;

        [PreserveName, PreserveCase]
        public static void Widget()
        {
            TimerText = jQuery.Select("#Timer").Html("");
            TimerWrapper = TimerText.Closest(".Gig").Hide();
            Message = jQuery.Select("#Message").Html("").Hide();

            Message.Html("").Hide().RemoveClass("Bigger");

            if (Storage.GetLocal(QuarterBellKey) == null)
                Storage.SetLocal(QuarterBellKey, true);
            QuarterBell = (bool) Storage.GetLocal(QuarterBellKey);

            Sound0 = jQuery.FromHtml("<audio preload='auto'/>").Attribute("src", BeginSound);
            Sound1 = jQuery.FromHtml("<audio preload='auto'/>").Attribute("src", BellSound);
            Sound2 = jQuery.FromHtml("<audio preload='auto'/>").Attribute("src", DoneSound);
            Sound4 = jQuery.FromHtml("<audio preload='auto'/>").Attribute("src", CatCallSound);

            WidgetTimer time1 = new WidgetTimer();
            WidgetTimer time2 = new WidgetTimer();

            time1.Duration = ReadyTime;
            time1.OnBegin = delegate
                            {
                                Running = true;
                                TimerWrapper.FadeIn(T1);
                                TimerText.Html("Get Ready").FadeIn(T1).FadeOut(T2);
                                Message.Html("").Hide();
                                ListEntry.StartList();
                            };
            time1.OnTick = delegate(WidgetTimer t)
                           {
                               if (t.TickDn == 2)
                               {
                                   TimerText.Html("Get Set").FadeIn(T1).FadeOut(T2);
                               }
                           };
            time1.OnEnd = delegate
                          {
                              WidgetTimer.PlaySound(Sound1, .75);
                              time2.StartEv();
                          };

            time2.Duration = Period;
            time2.OnBegin = delegate
                            {
                                TimerText.FadeIn(T1).Html("68");
                                Message.Html("GO").FadeIn(T1).FadeOut(T3);
                            };
            time2.OnTick = delegate(WidgetTimer t)
                           {
                               TimerText.Html(t.TickDn.ToString());
                           };
            time2.OnEnd = delegate
                          {
                              ListEntry.SaveData();
                              WidgetTimer.PlaySound(Sound2, .75);
                              Waiter = Window.SetTimeout(delegate
                                                         {
                                                             WidgetTimer.PlaySound(Sound4, .1);
                                                             Waiter = Window.SetTimeout(Cleanup, 1000);
                                                             TimerText.Html("Done");
                                                         }, 3500);
                          };

            jQuery.Select("#GoButton").Closest(".Gig")
                .Click(time1.StartEv)
                ;
        }

        public static void Cleanup()
        {
            Window.ClearTimeout(Waiter);
            Running = false;
        }

        public static void QuarterBellEv(jQueryEvent e)
        {
            QuarterBell = !QuarterBell;
            jQuery.Select("#QuarterBell").Html(QuarterBell ? "ON" : "OFF");
            Storage.SetLocal(QuarterBellKey, QuarterBell);
        }
    }

}